# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 14:29:42 2019

@author: ASUS
"""
print("-------------------------列表-----------------------")
x = list(range(20))
for i in range(len(x)):
    print(x[0])
    del x[0]
print(x)
x = [1,2,3,4,5,6]
y = x[::]
print(y)
print("-------------------------元组-----------------------")
print("----元组的创建与删除----")
a_tuple=('a',)
b_tuple=('a','b','c')
c_tuple=()#空元组
print(a_tuple)
print(b_tuple)
print(b_tuple)

a1 = 3
a2 = (3)
a3 = 3,
a4 = 1,2
print(a1)
print(a2)
print(a3)
print(a4)

d = 'abcde'
d_tuple = tuple('abcde')
aList = [-1,2,3.5,3,9,0,-8.7]
aList_tuple = tuple(aList)
s = tuple()
print(d)
print(d_tuple)
print(aList)
print(aList_tuple)
print(s)#空元组
'''
del s
print(s)
'''
#del(d_tuple[2])

print("----特殊情况:元组中包含可变序列----")
x = ([1,2],3)
x[0][0] = 5
print(x)
x[0].append(8)
#print(x)
#x[1] = 2

print("----序列解包----")
x,y,z = 1,2,3
print(x,y,z)

v_tuple = (False,3.2,'exp')
(x,y,z) = v_tuple#x,y,z = v_tuple

a = [1,2,3]
b,c,d = a
s = {'a':1,'b':2,'c':3}
b,c,d = s.items()#对键值对操作
b,c,d = s
b,c,d = s.values()#对值操作

keys = ['a','b','c','d']
values = [1,2,3,4]
for k,v in zip(keys,values):
    print(k,v)

print(*[1],*[2],3,*[4,5])
def demo (a,b,c,d):
    print(a,b,c,d)
demo(**{'a':1,'c':3},**{'b':2,'d':4})#调用函数时参数序列解包
f_tuple = *range(4),4
print(f_tuple)
f_list = [*range(4),4]
print(f_list)
z_dict = {*range(4),4,*(5,6,7)}
print(z_dict)
y_dict = {'x':1,**{'y':2}}
print(y_dict)

print("----生成器推导式----")
g = ((i + 2)**2 for i in range(10))
print(g)
print(tuple(g))#转为元组
print(tuple(g))#元素已经遍历结束
g = ((i + 2)**2 for i in range(10))
print(g.__next__())#单步迭代
print(g.__next__())#单步迭代
print(g.__next__())#单步迭代
g = ((i + 2)**2 for i in range(10))
for i in g:
    print(i,'',end='')

print("-------------------------字典-----------------------")
a = (1,2,3,4,5)#全局变量
b = 'Hello world'#全局变量
def demo():
    a = 3#局部变量
    b = [1,2,3]
    print('locals:',locals())
    print('globals:',globals())
demo()
print("----字典的创建与删除----")
a_dict = {'server':'diveintopython3.org','database':'mysql'}

keys = ['a','b','c','d']
values = [1,2,3,4]
dictionary = dict(zip(keys,values))
print(dictionary)
x = dict()#空字典
print(x)
x = {}#空字典
print(x)

d = dict(name='Zhang',age = 23)
adict = dict.fromkeys(['name','age','sex','genfer'])
print(adict)
#del(adict)  #删除整个字典
#del(adict['name']) #删除字典中的某一个键值对
#print(adict['name'])

print("----字典元素的读取----")
aDict = {'name':'Zhao','sex':'female','age':20}
print(aDict['name'])
#print(aDict['address'])
print(aDict.get('address'))
aDict['score'] = aDict.get('score',[])
aDict['score'].append(99)
aDict['score'].append(98)

for item in aDict.items():
    print(item)
print('')
for item in aDict.values():
    print(item)
print('')
for key in aDict:
    print(key)
print('')
for key,value in aDict.items():
    print(key,value)

print("----字典元素的添加与修改----")
aDict['age'] = 19
#aDict['address'] = 'Shaanxi'
aDict.update({'a':'a','b':'b'})
print(aDict)
#del(aDict['a'])
#aDict.clear()#删除字典中的所有元素
aDict_name = aDict.pop('age')#删除并返回指定"键"的元素
print(aDict_name)
aDict_item0 = aDict.popitem()#从后向前删除
aDict_item1 = aDict.popitem()
print("----字典应用案例----")
#首先生成包含1000个随机字符的字符串，然后统计每个字符的出现次数
import string 
import random
x = string.ascii_letters + string.digits + string.punctuation#字符，数字，标点
print(x)
y = [random.choice(x) for i in range(1000)]
z = ''.join(y)
d = dict()
print(y)
print(z)
for ch in z:
    d[ch] = d.get(ch,0)+1

print("-------------------------集合-----------------------")
print("----集合的创建与删除----")
a_set = {3,5}
a_set.add(4)
print(a_set)
a_set = set(range(8,14))
b_set = set([0,1,2,3,4,5,6,7])
x = set()#空集合
print(a_set)
print(b_set)
print(x)
b_set.add(8)
print(b_set)
#del(b_set)
#print(b_set)
b_set.pop()
print(b_set)
b_set.remove(2)
print(b_set)
b_set.clear()
print(b_set)
print("----集合操作----")
a_set = set([8,9,10])
b_set = set([0,1,2,10])
c0_set = a_set | b_set#并集
print(c0_set)
c1_set = a_set.union(b_set)#并集
print(c1_set)
d0_set = a_set & b_set#交集
print(d0_set)
d1_set = a_set.intersection(b_set)#交集
print(d1_set)
e0_set = a_set - b_set#差集
print(e0_set)
e1_set = a_set.difference(b_set)#差集
print(e1_set)

#去重
from random import randint
listRandom = [randint(0,9999) for i in range(100)]#100个介于0~9999之间的随机数
print(listRandom)
newSet = set(listRandom)
print(newSet)

















